<?php
// 后台用户模块
class SysUserAction extends CommonAction {
	function _filter(&$map){
		$map['id'] = array('egt',2);
		$map['account'] = array('like',"%".$_POST['account']."%");
	}
	// 检查帐号
	public function checkAccount() {
        if(!preg_match('/^[a-z]\w{4,}$/i',$_POST['account'])) {
            $this->error( '用户名必须是字母，且5位以上！');
        }
		$User = M("SysUser");
        // 检测用户名是否冲突
        $name  =  $_REQUEST['account'];
        $result  =  $User->getByAccount($name);
        if($result) {
        	$this->error('该用户名已经存在！');
        }else {
           	$this->success('该用户名可以使用！');
        }
    }
    
	// 插入数据
	public function insert() {
		// 创建数据对象
		$User	 =	 D("SysUser");
		$data['account'] = I('account',null);
		$data['nickname'] = I('nickname',null);
		$data['password'] = md5(I('password',null));
		$data['email'] = I('email',null);
		$data['status'] = I('status',null);
		$data['remark'] = I('remark',null);
		$User->data($data)->add();
		$this->success('用户添加成功！');
		// dump($data);exit;
		// if(!$User->create()) {
			// $this->error($User->getError());
		// }else{
			// 写入帐号数据
			// dump($_POST);exit;
			// $User->password = md5($_POST['password']);
			// if($result	 =	 $User->add()) {
				// $this->addRole($result);
				// $this->success('用户添加成功！');
			// }else{
				// $this->error('用户添加失败！');
			// }
		// }
	}
	
	public function forbid() {
		$User	 =	 D("SysUser");
		$id = $_REQUEST ['id'];
		$data['status'] = 0;
		$User->where("id=".$id)->save($data);
		// $this->success ( '状态禁用成功' );//不刷新页面
		$this->ajaxReturn('',"状态禁用成功!",1); //刷新页面
	}
	
	public function resume() {
		$User	 =	 D("SysUser");
		$id = $_REQUEST ['id'];
		$data['status'] = 1;
		$User->where("id=".$id)->save($data);
		// $this->success ( '状态恢复成功' );//不刷新页面
		$this->ajaxReturn('',"状态恢复成功!",1);//刷新页面
	}

    //重置密码
    public function resetPwd()
    {
    	$id  =  $_POST['id'];
        $password = $_POST['password'];
        if(''== trim($password)) {
        	$this->error('密码不能为空！');
        }
        $User = M('SysUser');
		$User->password	=	md5($password);
		$User->id			=	$id;
		$result	=	$User->save();
        if(false !== $result) {
            $this->success("密码修改为$password");
        }else {
        	$this->error('重置密码失败！');
        }
    }
    
    function foreverdelete(){
        $name=$this->getActionName();
        $model = D ($name);
        if (! empty ( $model )) {
            $pk = $model->getPk ();
            $id = $_REQUEST [$pk];
            if (isset ( $id )) {
                // check role_user
                if ($model->hasRole($id)){
                    $this->error ('删除失败,必须先删除此用户与角色的关联关系！');
                }

                $condition = array ($pk => array ('in', explode ( ',', $id ) ) );
                if (false !== $model->where ( $condition )->delete ()) {
                    //echo $model->getlastsql();
                    $this->success ('删除成功！');
                } else {
                    $this->error ('删除失败！');
                }
            } else {
                $this->error ( '非法操作' );
            }
        }
        $this->forward ();
    }
}
?>